package com.adinnet.api.common.config;

import com.adinnet.api.common.config.interceptor.OauthInterceptor;
import com.adinnet.common.upload.UploadConfig;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author: sunjianbo
 * @date: 2019/9/16 17:22
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new OauthInterceptor())
                .addPathPatterns("/**")                 //所有接口都拦截
                .excludePathPatterns("/api/v1/upload/uploadFile")//上传接口
                .excludePathPatterns("/api/user/login")//登录
                .excludePathPatterns("/api/user/update/pwd")//修改密码|重置密码
                .excludePathPatterns("/api/address/list")//省市区联动
                .excludePathPatterns("/api/user/check/verification")//校验验证码
                .excludePathPatterns("/api/user/check/token")//校验token有效性
                .excludePathPatterns("/api/link/submit")//提交意向信息
                .excludePathPatterns("/api/schoolWebsite/list/address")//分校区网点省市区联动
                .excludePathPatterns("/api/bootPage/getBootPage")//查询启动页接口
                .excludePathPatterns("/api/user/sendCode");   //过滤掉发送验证码接口
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowCredentials(true).allowedHeaders("*").allowedOrigins("*").allowedMethods("*");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**").addResourceLocations("file:" + UploadConfig.REAL_PATH);
    }
}
